package com.gzedu.module.controller.curd;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gzedu.common.sys.controller.BaseController;
import com.gzedu.common.util.page.JqPage;
import com.gzedu.common.util.page.Page;
import com.gzedu.module.faced.curd.CommonQueryFacade;
import com.gzedu.module.faced.curd.CurdButtonFacade;
import com.gzedu.module.faced.curd.CurdFormFacade;
import com.gzedu.module.faced.curd.CurdJsFacade;
import com.gzedu.module.faced.curd.CurdListFacade;
import com.gzedu.module.modle.curd.CurdButton;
import com.gzedu.module.modle.curd.CurdForm;
import com.gzedu.module.modle.curd.CurdList;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("/commonQuery")
public class CommonQueryController extends BaseController{
	@Autowired
	private CurdFormFacade curdFormFacade;
	@Autowired
	private CurdListFacade curdListFacade;
	@Autowired
	private CurdButtonFacade curdButtonFacade;
	@Autowired
	private CommonQueryFacade commonQueryFacade;
	@Autowired
	private CurdJsFacade curdJsFacade;
	
	/**
	 * 跳转到通用列表页面
	 * @param request
	 * @param response
	 * @param curd_form_id
	 * @param model
	 * @return
	 */
	@RequestMapping("/commonQuery")
	public String commonQuery(HttpServletRequest request,HttpServletResponse response,String curd_form_id,Model model){
		List<CurdButton> btnInfo = curdButtonFacade.getBtnInfo(curd_form_id);
		String modulePath = curdFormFacade.getModulePath(curd_form_id);
		String jsContent = curdJsFacade.getJsContent(curd_form_id);
		System.out.println(jsContent);
		model.addAttribute("form_id",curd_form_id);
		model.addAttribute("btnInfo", btnInfo);
		model.addAttribute("modulePath", modulePath);
		model.addAttribute("jsContent", jsContent);
		return "/views/curd/commonList";
	}
	
	/**
	 * 通用列表设置信息
	 * @param request
	 * @param response
	 * @param curdList
	 * @param model
	 * @return
	 */
	@RequestMapping("/listQuery")
	@ResponseBody
	public String listQuery(HttpServletRequest request,HttpServletResponse response,CurdList curdList , Model model){
		List<Map<String,Object>> curdListRes = curdListFacade.selectByFormId(curdList);
		JSONArray json = JSONArray.fromObject(curdListRes);
		return json.toString();
	}
	
	/**
	 * 通用列表表单信息设置
	 * @param request
	 * @param response
	 * @param curdForm
	 * @return
	 */
	@RequestMapping("/formQuery")
	@ResponseBody
	public String formQuery(HttpServletRequest request,HttpServletResponse response,CurdForm curdForm){
		List<CurdForm> res = curdFormFacade.selectByFormId(curdForm);
		JSONArray json = JSONArray.fromObject(res);
		return json.toString();
	}
	
	/**
	 * 根据sql查询数据
	 * @param request
	 * @param response
	 * @param sql
	 * @return
	 */
	@RequestMapping("/dataQuery")
	@ResponseBody
	public String queryData(HttpServletRequest request,HttpServletResponse response,String sql,Page page,String orderBy){
		String pages = request.getParameter("page"); // 取得当前页数,注意这是jqgrid自身的参数   
		page.setPageNo(Integer.parseInt(pages));
		JqPage jp = commonQueryFacade.dataQuery(sql,orderBy, page);
		JSONArray json = JSONArray.fromObject(jp);
		System.out.println(json.toString());
		String subStr = json.toString().substring(1 , json.toString().length() - 1);
		return subStr;
	}
	
	@RequestMapping("/insertCommonQueryInfo")
	@ResponseBody
	public Map<String,Object> insertCommonQueryInfo(HttpServletRequest request,HttpServletResponse response,CurdForm curdForm){
		return commonQueryFacade.insertCommonQuery(request,curdForm);
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Map<String,Object> update(HttpServletRequest request,HttpServletResponse response){
		return commonQueryFacade.update(request);
	}
	
	
}
